package org.lionel.clazz;

import lombok.SneakyThrows;

import java.util.LinkedList;

public class Stack {

    private static LinkedList list = new LinkedList();


    public synchronized void push(Object x) {
        synchronized (list) {
            list.addLast(x);
            notify();
        }
    }

    public synchronized Object pop()
            throws Exception {
        synchronized (list) {
            if (list.size() <= 0) {
                wait();
            }
            return list.removeLast();
        }
    }

    public static void main(String[] args) {
        for (Integer i = 0; i < 10; i++) {
            Integer a = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("add:" + a);
                    new Stack().push(a);
                }
            }).start();
        }
        for (Integer j = 0; j < 10; j++) {
            Integer b = j;
            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    System.out.println("bbb");
                    System.out.println("remove:" + new Stack().pop());
                }
            }).start();
        }
    }
}
